/**
 * @description 所有人可使用的参数配置列表
 * @params hideMenu: 是否隐藏当前路由结点不在导航中展示
 * @params alwayShow: 只有一个子路由时是否总是展示菜单，默认false
 */
import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";
import NProgress from "@/utils/system/nprogress";

// 动态路由相关引入数据
// import Layout from "@/layout/index.vue";
// import MenuBox from "@/components/menu/index.vue";
// import { createNameComponent } from "./createNode";

// 引入modules
import Dashboard from "./modules/dashboard";
import Pages from "./modules/pages";
import System from "./modules/system";
// import { ElMessage } from "element-plus";

let modules = [...System];

const routes = modules;

const router = createRouter({
  history: createWebHistory(),
  routes,
});
let asyncRoutes = [...Dashboard, ...Pages];
// 动态路由的权限新增，供登录后调用
export function addRoutes() {
  // 与后端交互的逻辑处理，处理完后异步添加至页面
  asyncRoutes.forEach((item) => {
    modules.push(item);
    router.addRoute(item);
  });
}

if (store.state.user.token) {
  addRoutes();
}

router.afterEach((to) => {
  const keepAliveComponentsName =
    store.getters["keepAlive/keepAliveComponentsName"] || [];
  const name = to.matched[to.matched.length - 1].components.default.name;
  if (
    to.meta &&
    to.meta.cache &&
    name &&
    !keepAliveComponentsName.includes(name)
  ) {
    store.commit("keepAlive/addKeepAliveComponentsName", name);
  }
  NProgress.done();
});

export { modules };

export default router;
